// pages/index/index.js
const app = getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftHight: 0,
    rightHight: 0,
    GoodsLeftList: [],
    GoodsRightList: []
  },
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  AddCommodity:function(e){
    wx.navigateTo({
      url: "/pages/ShopAdmin/AddCommodity/AddCommodity",
    })
  },
  ModifyCommodity:function(e){
    console.log(e)
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: "/pages/ShopAdmin/ModifyCommodity/ModifyCommodity?id=" +e.currentTarget.id+ "",
    })
  },
  
  getxinxi: function () {
    var that = this
    // 必须是在用户已经授权的情况下调用
    wx.getUserInfo({
      success: function (res) {
        console.log(res)
        console.log(res.userInfo.nickName)

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Goods()
    console.log(app.user.openid)

  },
  Goods: function () {
    var that = this
    that.setData({
      leftHight: 0,
      rightHight: 0,
      GoodsLeftList: [],
      GoodsRightList: []
    })
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    wx.cloud.callFunction({
      name: "login"
    }).then(res => {
      console.log(res.result.openid);
      // 查询数据
      db.collection('Goods')
        .where({
          _openid: res.result.openid,
        }).get()
        .then(res => {
          // res.data 包含该记录的数据
          console.log(res.data)
          if (res.data.length == 0) {
            wx.showToast({
              title: "已无更多商品",
              icon: "none"
            })
          }
          var leftH = that.data.leftHight;
          var rightH = that.data.rightHight;
          let L = 0;
          let R = 0;
          console.log(leftH)
          console.log(rightH)
          console.log(L)
          console.log(R)
          for (let i = 0; i < res.data.length; i++) {
            wx.getImageInfo({
              src: res.data[i].fileIDs[0],
              success: function (res2) {
                console.log(res2.height)
                console.log(res2.width)
                var height2 = parseInt(Math.round(res2.height * 345 / res2.width))
                console.log(height)
                if (leftH == rightH || leftH < rightH) {
                  leftH += res2.height;
                  var id = "GoodsLeftList[" + L + "].id"
                  var title = "GoodsLeftList[" + L + "].title"
                  var color = "GoodsLeftList[" + L + "].color"
                  var fileid = "GoodsLeftList[" + L + "].fileid"
                  var numbers = "GoodsLeftList[" + L + "].numbers"
                  var rmb = "GoodsLeftList[" + L + "].rmb"
                  var size = "GoodsLeftList[" + L + "].size"
                  var state = "GoodsLeftList[" + L + "].state"
                  var show = "GoodsLeftList[" + L + "].show"
                  var sale = "GoodsLeftList[" + L + "].sale"
                  var height = "GoodsLeftList[" + L + "].height"
                  that.setData({
                    [title]: res.data[i].title,
                    [id]: res.data[i]._id,
                    [fileid]: res.data[i].fileIDs[0],
                    [rmb]: res.data[i].rmb,
                    [state]: res.data[i].state,
                    [size]: res.data[i].size,
                    [numbers]: res.data[i].numbers,
                    [color]: res.data[i].color,
                    [show]: false,
                    [sale]: res.data[i].sale,
                    [height]: height2 + "rpx",
                    leftHight: leftH,
                  })
                  console.log("lalalala")
                  L = L + 1
                  console.log(that.data.GoodsLeftList)
                } else {
                  rightH += res2.height;
                  var id = "GoodsRightList[" + R + "].id"
                  var title = "GoodsRightList[" + R + "].title"
                  var color = "GoodsRightList[" + R + "].color"
                  var fileid = "GoodsRightList[" + R + "].fileid"
                  var numbers = "GoodsRightList[" + R + "].numbers"
                  var rmb = "GoodsRightList[" + R + "].rmb"
                  var size = "GoodsRightList[" + R + "].size"
                  var state = "GoodsRightList[" + R + "].state"
                  var show = "GoodsRightList[" + R + "].show"
                  var sale = "GoodsRightList[" + R + "].sale"
                  var height = "GoodsRightList[" + R + "].height"
                  that.setData({
                    [title]: res.data[i].title,
                    [id]: res.data[i]._id,
                    [fileid]: res.data[i].fileIDs[0],
                    [rmb]: res.data[i].rmb,
                    [state]: res.data[i].state,
                    [size]: res.data[i].size,
                    [numbers]: res.data[i].numbers,
                    [color]: res.data[i].color,
                    [show]: false,
                    [sale]: res.data[i].sale,
                    [height]: height2 + "rpx",
                    rightHight: rightH,
                  })
                  R = R + 1
                  console.log(that.data.GoodsRightList)
                }
                if (i == res.data.length - 1) {
                  wx.hideLoading();
                }
              }
            })
          }

        })
        .catch(err => {
          console.error(err)
          wx.hideLoading();
        })
    }).catch(err => {
      console.log(err);
      wx.hideLoading();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.Goods()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
    * 弹窗
    */
  sale: function (e) {
    console.log(e.target.id)
    parseInt(e.target.id)
    var that = this
    var sale = that.data.GoodsLeftList[parseInt(e.target.id)].sale
    var rmb = that.data.GoodsLeftList[parseInt(e.target.id)].rmb
    console.log(sale)
    let rmb2 = (sale / 10 * rmb).toFixed(2)
    that.setData({
      showModal: true,
      sale: that.data.GoodsLeftList[parseInt(e.target.id)].sale,
      rmb: that.data.GoodsLeftList[parseInt(e.target.id)].rmb,
      number: parseInt(e.target.id),
      rmb2: rmb2,
      LeftRight: 0
    })

  },
  sale2: function (e) {
    console.log(e.target.id)
    parseInt(e.target.id)
    var that = this
    var sale = that.data.GoodsRightList[parseInt(e.target.id)].sale
    var rmb = that.data.GoodsRightList[parseInt(e.target.id)].rmb
    let rmb2 = (sale / 10 * rmb).toFixed(2)
    that.setData({
      showModal: true,
      sale: that.data.GoodsRightList[parseInt(e.target.id)].sale,
      rmb: that.data.GoodsRightList[parseInt(e.target.id)].rmb,
      rmb2: rmb2,
      number: parseInt(e.target.id),
      LeftRight: 1
    })

  },
  // 获取输入内容
  inputChange: function (e) {
    var that = this
    let value = e.detail.value.replace(/\D/g, '')
    var rmb = parseFloat(value)
    var sale = that.data.sale
    let rmb2 = (sale / 10 * rmb).toFixed(2)
    this.setData({
      rmb: rmb,
      rmb2: rmb2,
    })
  },
  // 获取输入内容
  inputChange2: function (e) {
    // var money;
    // if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) { //正则验证，提现金额小数点后不能大于两位数字
    //   money = e.detail.value;
    // } else {
    //   money = e.detail.value.substring(0, e.detail.value.length - 1);
    // }
    // console.log(money)

    var that = this
    let value = e.detail.value.replace(/\D/g, '')
    var sale = parseInt(value)
    var rmb = that.data.rmb
    let rmb2 = (sale / 10 * rmb).toFixed(2)
    this.setData({
      sale: sale,
      rmb2: rmb2,
    })
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false,
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();

  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var that = this
    if (that.data.LeftRight == 0) {
      db.collection("Goods").doc(that.data.GoodsLeftList[that.data.number].id).update({
        data: {
          sale: that.data.sale,
          rmb: that.data.rmb
        },
        success: function (res) {
          console.log(res.data)
          that.hideModal();
          var sale = "GoodsLeftList[" + that.data.number + "].sale"
          var rmb = "GoodsLeftList[" + that.data.number + "].rmb"
          that.setData({
            [sale]: that.data.sale,
            [rmb]: that.data.rmb
          })
          console.log(that.data.GoodsLeftList[that.data.number].rmb)
          wx.showToast({
            title: "打折成功",
            icon: "none"
          })
        }
      });
    } else {
      db.collection("Goods").doc(that.data.GoodsRightList[that.data.number].id).update({
        data: {
          sale: that.data.sale,
          rmb: that.data.rmb
        },
        success: function (res) {
          console.log(res.data)
          that.hideModal();
          var sale = "GoodsRightList[" + that.data.number + "].sale"
          var rmb = "GoodsRightList[" + that.data.number + "].rmb"
          that.setData({
            [sale]: that.data.sale,
            [rmb]: that.data.rmb
          })
          wx.showToast({
            title: "打折成功",
            icon: "none"
          })
        }
      });
    }
    // if (this.data.address != '') {
    //   this.hideModal();
    //   this.setData({
    //     checked: false,
    //     checked2: true
    //   });
    // } else {
    //   wx.showToast({
    //     title: "由于你的交易方式是自提，你必须输入自提地址",
    //     icon: "none"
    //   })
    // }
  }
})